package com.mango.avtiveMQ;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @Author:  MangoCookieMilk
 * @Date:     2021/2/28 13:48
 */
public class ActiveMQConsumer {

    public static final String ACTIVE_URL="tcp://101.201.249.141:61616";
    public static final String QUEUE_NAME="queue01";
    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVE_URL);
        //通过连接工厂获取connection连接 并启动访问
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();
        //创建会话session  需要两个参数，第一个事务，第二个签收
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //创建目的地(选择是队列还是主题)
        Queue queue = session.createQueue(QUEUE_NAME);
        //创建消息的消费者
        MessageConsumer messageConsumer = session.createConsumer(queue);
        while (true){
            //从队列中获取消息  receive未设置最大时间 是阻塞的，
            TextMessage textMessage = (TextMessage) messageConsumer.receive();
            if (textMessage !=null){
                System.out.println("消费者接受到消息---->"+textMessage.getText());
            }else {
                break;
            }
        }
        messageConsumer.close();
        session.close();
        connection.close();
    }
}
